package cn.xorange.commons.core.auth.handler;

import cn.hutool.core.date.DateUtil;
import cn.xorange.commons.core.auth.mapper.UserInfoMapper;
import cn.xorange.commons.security.constants.HeaderConstants;
import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.enums.SysType;
import cn.xorange.commons.security.filter.FilterHandler;

import cn.xorange.commons.security.mapper.LoginUserMapper;
import cn.xorange.commons.security.service.SecurityService;
import cn.xorange.commons.utils.http.ServletUtils;
import cn.xorange.commons.utils.id.IdUtils;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author : yangjian
 * @date : 2023/12/23
 */
public class FilterUserInfoHandler implements FilterHandler {

    @Resource
    SecurityService securityService;
    @Value("${mickey.app-username-prefix:#{null}}")
    public String appUsernamePrefix;
    @Autowired
    UserInfoMapper mapper;
    @Resource
    LoginUserMapper loginUserMapper;


    @Override
    public String url() {
        return SecurityConstants.URL_USERINFO;
    }

    @Override
    public Boolean passToken() {
        return false;
    }

    @Override
    public void handler(HttpServletRequest request, HttpServletResponse response) {

        String loginType = request.getHeader(HeaderConstants.X_SYS_TYPE);
        SecurityUser user = securityService.getLoginUser();
        HashMap<String, Serializable> map = new HashMap<>();
        if(SysType.APP.type().equals(loginType)){
            SecurityUser user2 = mapper.selectAppUserInfo(user.getUserId());
            if(Objects.isNull(user2)){
                String nickname = this.appUsernamePrefix + IdUtils.randomNum(4);
                loginUserMapper.insertNicknameForAppUserProfile(user.getUserId() ,nickname, DateUtil.date());
                user2 = mapper.selectAppUserInfo(user.getUserId());
            }
            securityService.refreshUserInfoCache(JSON.toJSONString(user2));

            map.put(SecurityConstants.NICK_NAME, user2.getNickname());
            map.put(SecurityConstants.ORANGE_ID, user2.getOrangeId());
            map.put(SecurityConstants.USER_AVATAR, user2.getAvatar());
            map.put(SecurityConstants.USER_SEX, user2.getSex());
            map.put(SecurityConstants.USER_BIRTHDAY, user2.getBirthday());
            map.put(SecurityConstants.USER_ZODIAC,user2.getZodiac());
            map.put(SecurityConstants.USER_STAR,user2.getStar());
            map.put(SecurityConstants.USER_CITY, user2.getCity());
        }else {
            map.put(SecurityConstants.USER_NAME, user.getUsername());
            map.put(SecurityConstants.USER_AVATAR, user.getAvatar());
            map.put(SecurityConstants.USER_TENANT_ID, user.getTenantId());
            map.put("roles", user.getRoleKeys());
            map.put("permissions", user.getPermissions());
        }
        ServletUtils.responseSuccessOut(response, map);

    }
}
